/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "napi/native_api.h"
#include "lottie_c.h"
#include <cstdlib>
#include <string>
#include "securec.h"

void *g_context = nullptr;
static napi_value InitLottie(napi_env env, napi_callback_info info)
{
    if (g_context != nullptr) {
        return nullptr;
    }
    size_t argc = 1;
    napi_value args[1] = {nullptr};

    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
    
    CLottieConfig *config = CreateCLottieConfig();
    memcpy_s(config->path, strlen("lottie.json"), "lottie.json", strlen("lottie.json"));
    config->autoplay = true;
    g_context = LottieCreateContext(args[0], config);
    DestroyCLottieConfig(config);
    LottieCreate(g_context);
    
    typedef struct {
        double number;
        void *context;
    } Data;
    const double defaultSpeed = -0.5;
    auto mydata = (Data *)malloc(sizeof(Data));
    if (mydata == nullptr) {
        return nullptr;
    }
    mydata->context = g_context;
    mydata->number = defaultSpeed;
    
    LottieAddListeningFunctionByName(g_context, "data_ready", [](void *data) {
        auto mydata = (Data *)(data);
        LottieSetSpeed(mydata->context, mydata->number);
        free(mydata);
        mydata = nullptr;
    }, mydata);
    
    return nullptr;
}

static napi_value PlayLottie(napi_env env, napi_callback_info info)
{
    const double speed = 2;
    if (g_context != nullptr) {
        LottiePlay(g_context);
        LottieSetSpeed(g_context, speed);
    }
    return nullptr;
}

static napi_value PauseLottie(napi_env env, napi_callback_info info)
{
    if (g_context != nullptr) {
        LottiePause(g_context);
    }
    return nullptr;
}

static napi_value DestroyLottie(napi_env env, napi_callback_info info)
{
    if (g_context != nullptr) {
        LottieDestroy(g_context);
        LottieDestroyContext(g_context);
        g_context = nullptr;
    }
    return nullptr;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
    InitLottieComponentEnvironment(env);
    napi_property_descriptor desc[] = {
        { "initLottie", nullptr, InitLottie, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "playLottie", nullptr, PlayLottie, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "pauseLottie", nullptr, PauseLottie, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "destroyLottie", nullptr, DestroyLottie, nullptr, nullptr, nullptr, napi_default, nullptr }
    };
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "examplecpp",
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterExampleCPPModule(void)
{
    napi_module_register(&demoModule);
}
